define(['jquery', 'bootstrap', 'backend', 'table', 'form','CreateControl'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        _queryString: '',
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tvsd/pc/index' + Controller._queryString,
                    add_url: 'tvsd/pc/add' + Controller._queryString,
                    edit_url: 'tvsd/pc/edit',
                    del_url: 'tvsd/pc/del',
                    dy_url: 'tvsd/pc/dy',
                    multi_url: 'tvsd/pc/multi',
                    import_url: 'tvsd/pc/import',
                    table: 'dd_pc',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        {
                            field: '', title: __('序号'), formatter: function (value, row, index) {
                                var options = table.bootstrapTable('getOptions');
                                var pageNumber = options.pageNumber;

                                var pageSize = options.pageSize == "All" ? 100000 : options.pageSize;

                                return (pageNumber - 1) * pageSize + 1 + index;
                            }, operate: false
                        },
                        // { field: 'id', title: __('Id') },
                        { field: 'ddh', title: __('Ddh'), operate: 'LIKE' },
                        { field: 'kh', title: __('Kh'), operate: 'LIKE' },
                        { field: 'cp', title: __('Cp'), operate: 'LIKE' },
                        { field: 'kstime', title: __('Kstime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        { field: 'jstime', title: __('Jstime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        { field: 'lxr', title: __('Lxr'), operate: 'LIKE' },
                        { field: 'dh', title: __('Dh'), operate: 'LIKE' },
                        { field: 'cd', title: __('Cd'), operate: 'LIKE' },
                        
                        { field: 'zws', title: __('Zws'), operate: 'LIKE' },
                        { field: 'jsy1', title: __('Jsy1'), operate: 'LIKE' },
                        // {field: 'jsydh1', title: __('Jsydh1'), operate: 'LIKE'},
                        { field: 'jsy2', title: __('Jsy2'), operate: 'LIKE' },
                        // {field: 'jsydh2', title: __('Jsydh2'), operate: 'LIKE'},
                       
                        { field: 'scd', title: __('Scd'), operate: 'LIKE' },
                        { field: 'tj', title: __('Tj'), operate: false },
                        { field: 'mdd', title: __('Mdd'), operate: 'LIKE' },
                        { field: 'je', title: __('Je') },
                        { field: 'bz', title: __('Bz'), operate: false },
                        { field: 'status', title: __('Status'), searchList: { "未报销": __('未报销'), "已报销": __('已报销') }, formatter: Table.api.formatter.status },
                        { field: 'ddr', title: __('Ddr'), operate: 'LIKE' },
                        // {field: 'admin_id', title: __('Admin_id')},
                        { field: 'weigh', title: __('Weigh'), operate: false },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('按钮组'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('报销'),
                                    title: __('报销'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'example/bootstraptable/detail',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                // {
                                //     name: 'ajax',
                                //     text: __('发送Ajax'),
                                //     title: __('发送Ajax'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                //     icon: 'fa fa-magic',
                                //     url: 'example/bootstraptable/detail',
                                //     confirm: '确认发送',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                //         //如果需要阻止成功提示，则必须使用return false;
                                //         //return false;
                                //     },
                                //     error: function (data, ret) {
                                //         console.log(data, ret);
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     }
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('新选项卡中打开'),
                                //     title: __('新选项卡中打开'),
                                //     classname: 'btn btn-xs btn-warning btn-addtabs',
                                //     icon: 'fa fa-folder-o',
                                //     url: 'example/bootstraptable/detail'
                                // },
                                {
                                    name: 'dy',
                                    text: __('打印'),
                                    title: __('打印'),
                                    classname: 'btn btn-xs btn-warning dy',
                                    icon: 'fa fa-folder-o',
                                    // url: 'example/bootstraptable/detail'
                                    // click:window.open('pc/dy')
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    
                    
                    
                    ]
                ]
            });
           //点击打印按钮触发代码
           $(document).on("click", ".dy", function () {
            // var options = table.bootstrapTable('getOptions');
            // var search = options.queryParams({});
            // var filter = search.filter;
            // var op = search.op;

            // //设置cookie存储的值
            // $.cookie('filter', filter);
            // $.cookie('op', op);

            //打开新标签页
            // window.open('dtjk/dy?&sort=sjtime&order=ASC&limit=100000&filter=' + filter + '&op=' + op);
            window.open('pc/dy');
            // location.href = 'dtjk/dy';

            //跳转页面
            // location.href = 'dtjk/dy' + '?filter=' + filter + '&op=' + op;


        });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'tvsd/pc/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'tvsd/pc/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'tvsd/pc/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //打印页面
        dy: function () {
            //网页运行完成后
            $(function () {
            //     //读取cookie存储的值
            //     var filter = $.cookie('filter');
            //     var op = $.cookie('op');
            //     // $.setCookie('filter');
            //     // $.setCookie('op');
            //     //取网页传参，取？后面部分包括？
            //     // var location = window.location.search;

            //     var reportViewer = rubylong.grhtml5.insertReportViewer("report_holder", urlAddRandomNo("/static/grf/1a.grf"), 'dysj' + '?filter=' + filter + '&op=' + op);
            //     reportViewer.start();

            
           
                // var location = window.location.search;

                // var filter = $.cookie('filter');
                // var op = $.cookie('op');
                InsertPrintViewer({
                   //取网页传参，取？后面部分包括？
                
                    report: urlAddRandomNo("/assets/派车调度单.grf"),
                    data: 'dy'
                });
           });

            

        },
        add: function () {
            //页面加载完成，添加订单号
            $(document).ready(function () {
                $('#c-ddh').val(createordernum());
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                //用车单位被选中触发(值被改变)，读取选中数据，填充联系人
                $('#c-kh').data('eSelect', function (data) {
                    //输出当前选中行的json数据

                    // console.log(data);
                    Fast.api.ajax({
                        url: 'tvsd/kh/lxr',
                        data: { id: data['id'] }
                    }, function (data, ret) {
                        // console.log(data);
                        if (ret.msg == 'ok') {
                            // console.log(data.lxr);
                            $("#c-lxr").val(data.lxr);
                            $("#c-dh").val(data.dh);
                            return false;
                        } else {
                            // console.log("2222");
                            return false;
                        }
                        // return false;
                    }, function (ret) { //error
                        // console.log(ret);
                        // console.log("333");
                        return false;
                    });
                });

                //所属车队被选中触发(值被改变)，读取选中数据，填充联系人
                $('#c-cd').data('eSelect', function (data) {
                    //输出当前选中行的json数据
                    // console.log(data);
                    Fast.api.ajax({
                        url: 'tvsd/cl/cp',
                        data: { id: data['id'] }
                    }, function (data, ret) {
                        // console.log(data);
                        if (ret.msg == 'ok') {
                            // console.log(data.lxr);
                            $('#c-cp').selectPageData(data);
                            return false;
                        } else {
                            // console.log("2222");
                            return false;
                        }
                        // return false;
                    }, function (ret) { //error
                        // console.log(ret);
                        // console.log("333");
                        return false;
                    });
                });
                //车牌号被选中触发(值被改变)，读取选中数据，填充座位数
                $('#c-cp').data('eSelect', function (data) {
                    $('#c-zws').val(data['zws']);
                });

                //驾驶员1被选中触发，读取选中数据，返回填充电话
                $("#c-jsy1").data('eSelect', function (data) {
                    Fast.api.ajax({
                        url: 'tvsd/jsy/jsy',
                        data: { id: data['id'] }
                    }, function (data, ret) {
                        // console.log(data);
                        if (ret.msg == 'ok') {
                            $("#c-jsydh1").val(data.dh);//返回数据填充座位数
                            return false;
                        } else {
                            return false;
                        }
                    }, function (ret) { //error
                        return false;
                    });
                });
                //驾驶员1被选中触发，读取选中数据，返回填充电话
                $("#c-jsy2").data('eSelect', function (data) {
                    Fast.api.ajax({
                        url: 'tvsd/jsy/jsy',
                        data: { id: data['id'] }
                    }, function (data, ret) {
                        // console.log(data);
                        if (ret.msg == 'ok') {
                            $("#c-jsydh2").val(data.dh);//返回数据填充座位数
                            return false;
                        } else {
                            return false;
                        }
                    }, function (ret) { //error
                        return false;
                    });
                });

                Form.api.bindevent($("form[role=form]"));
            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});

function setTimeDateFmt(s) {  // 个位数补齐十位数
    return s < 10 ? '0' + s : s;
}
//基于年月日时分秒+随机数生成订单编号
function createordernum() {
    const now = new Date()
    let month = now.getMonth() + 1
    let day = now.getDate()
    let hour = now.getHours()
    let minutes = now.getMinutes()
    let seconds = now.getSeconds()
    month = setTimeDateFmt(month)
    day = setTimeDateFmt(day)
    hour = setTimeDateFmt(hour)
    minutes = setTimeDateFmt(minutes)
    seconds = setTimeDateFmt(seconds)
    let orderCode = now.getFullYear().toString() + month.toString() + day + hour + minutes + seconds + (Math.round(Math.random() * 1000000)).toString();
    return orderCode;
    //基于年月日时分秒+随机数生成订单编号
}